package com.example.shortmessage.ui.activity;

import android.os.Build;
import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.shortmessage.adapter.MyContentAdapter;
import com.example.shortmessage.base.BaseActivity;
import com.example.shortmessage.base.BaseStyle;
import com.example.shortmessage.contract.DetailsContract;
import com.example.shortmessage.data.constant.DetailsConst;
import com.example.shortmessage.data.constant.FontsConst;
import com.example.shortmessage.data.entity.MessageData;
import com.example.shortmessage.databinding.ActivityDetailsBinding;
import com.example.shortmessage.presenter.DetailsPresenter;
import com.example.shortmessage.tips.dialog.WaitingDialog;

import java.util.List;

public class DetailsActivity extends BaseActivity<ActivityDetailsBinding, DetailsContract.View<DetailsContract.Presenter>,DetailsContract.Presenter>
        implements DetailsContract.View<DetailsContract.Presenter> {

    private List<MessageData> data;

    @Override
    protected DetailsContract.Presenter initPresenter() {
        return new DetailsPresenter(this);
    }

    @Override
    protected ActivityDetailsBinding getLayoutView() {
        return ActivityDetailsBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        String number = getIntent().getStringExtra(DetailsConst.SIGN);
        mBinding.title.tvTitle.setText(number);
        data = mPresenter.getData(number);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            data.sort((o1,o2)-> (int)(o1.getDate()-o2.getDate()));
        }
        MyContentAdapter myContentAdapter = new MyContentAdapter(data, this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        mBinding.rvContent.setLayoutManager(linearLayoutManager);
        mBinding.rvContent.setAdapter(myContentAdapter);
        mBinding.rvContent.scrollToPosition(myContentAdapter.getItemCount()-1);
        mBinding.btnSeach.setOnClickListener(v -> {
            String string = mBinding.etText.getText().toString();
            mPresenter.sendMessage(number,string);
            mBinding.etText.setText("");
        });
    }

    @Override
    protected void setStyle() {
        mBinding.title.tvTitle.setVisibility(View.VISIBLE);
        BaseStyle baseStyle = new BaseStyle();
        baseStyle.set(this, mBinding.title.tvTitle, FontsConst.TV_TITLE);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            MainActivity.MainActivityInstance(this);
            WaitingDialog.getInstance().show(this);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /***********************************************************************/

    @Override
    public void setPresenter(@NonNull DetailsContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void showLoading() {
        WaitingDialog.getInstance().show(this);
    }

    @Override
    public void hideLoading() {
        WaitingDialog.getInstance().dismiss();
    }

    @Override
    public void onError(Throwable throwable) {

    }
}
